<template>
    <div class='bgg' :style="{'backgroundImage':'url(' + (loginBg?'/api/upload/'+loginBg:require('../../assets/images/pic.png')) + ')'}">
        <div class="ms-title">{{sysTitle}}</div>
        <div class="ms-login">
            <el-form @submit.native.prevent :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <input v-model="ruleForm.username" class="input_style user_bg"/>
                </el-form-item>
                <el-form-item prop="password" class="ps_style">
                    <input type="password" v-model="ruleForm.password" @keyup.enter="submitForm('ruleForm')" class="input_style pass_bg"/>
                    <img src="../../assets/images/login_icon_keyboard.png" alt="" class="keyImg" v-if="showKeyBoard">
                </el-form-item>
                <div class="login-btn">
                    <el-button :loading=loading type="primary" @click="submitForm('ruleForm')" class="btn_style">登录</el-button>
                </div>
            </el-form>
        </div>
        <div class="copyright">copyright&copy; 南京英普得企业管理咨询有限公司All rights reserved</div>
    </div>
</template>
<script>
import { mapState } from 'vuex'
    export default {
        data: function(){
            return {
                loading:false,
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                },
                showKeyBoard: true
            }
        },
        computed: mapState({
            msg: state => state.login.msg,
            sessionId: state => state.login.sessionId,
            pageType: state => state.login.pageType,
            userName: state => state.login.userName,
            userType: state => state.login.userType,
            retcode: state => state.login.retCode,
            sysTitle: state => state.system.systemTitle,
            loginBg: state => state.system.loginBg
        }),
        methods: {
            submitForm(formName) {
                var self = this;
                self.loading = true
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$store.dispatch('login',{
                            loginName: this.ruleForm.username,
                            password: this.ruleForm.password
                        }).then(() => {
                            if(this.retcode == 0) {
                            //把session保存在本地
                                localStorage.setItem('sessionId',this.sessionId);
                                localStorage.setItem('userName',this.userName);
                                localStorage.setItem('pageType',this.pageType);
                                localStorage.setItem('userType',this.userType);
                                if(this.pageType == 1) {
                                    this.$router.push({ path:'/dashboard' });
                                }else if(this.pageType == 0){
                                    this.$router.push({ path:'/user' });
                                }
                            }else{
                                this.$message(this.msg);
                            }
                            self.loading = false
                        })
                    } else {
                        self.loading = false
                        return false;
                    }
                });
            },
            getTitle() {
                this.$store.dispatch('getSysTitle',{
                    sessionId: localStorage.getItem('sessionId')
                })
            }
        },
        mounted() {
            const htmlDom = document.getElementsByTagName('html')[0];
            htmlDom.removeAttribute('style');
            if(navigator.userAgent.toLowerCase().indexOf('se 2.x') !== -1) {
                this.showKeyBoard = false;
            }
        },
        created() {
            this.getTitle();
        }
    }
</script>

<style scoped>
    html{
        font-size: 14px;
    }
    .bgg{
        position: relative;
        width: 100%;
        height: 100%;
        background-repeat: no-repeat;
        background-size: 100% 72%;
    }
    .ms-title{
        position: absolute;
        top:60%;
        width:100%;
        margin-top: -230px;
        text-align: center;
        font-size:30px;
        color: #232323;
    }
    .ms-login{
        position: absolute;
        left:50%;
        top:58%;
        width:300px;
        margin:-150px 0 0 -190px;
        padding:40px;
        border-radius: 8px;
        background: #fff;
        box-shadow: 0 1px 12px rgba(129,145,166,0.5);
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width: 100%;
        height: 40px;
        /* line-height: 40px; */
        background-color: #232323;
        color: #fff;
        border: none;
        border-radius: 20px;
        font-size: 16px;
        cursor: pointer;
    }
    .el-input__inner{
        border: 1px solid #d0d0d0;
        height: 30px;
        line-height: 30px;
        border-radius: 15px;
    }
    .input_style{
        width: 84%;
        height: 40px;
        line-height: 40px;
        border: 1px solid #d0d0d0;
        border-radius: 20px;
        outline: 0;
        padding-left: 20px;
        margin-bottom: 15px;
        padding-left: 47px;
        color: #5e5e5e;
    }
    .user_bg{
        background: url('../../assets/images/login_icon_username.png') 0 0 no-repeat;
        background-size: 9%;
        background-position: 10px 6px;
    }
    .pass_bg{
        background: url('../../assets/images/login_icon_password.png') 0 0 no-repeat;
        background-size: 9%;
        background-position: 10px 6px;
    }
    .copyright{
        color: #777c8a;
        font-size: 12px;
        position: fixed;
        bottom: 70px;
        width: 100%;
        text-align: center;
    }
    .ps_style{
        position: relative;
    }
    .keyImg{
        position: absolute;
        right: 10px;
        top: 4px;
        width: 30px;
    }
</style>